/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */
//表情键盘toolbar
import UIKit

enum LSEmoticonToolBarButtonType: Int {
    //默认表情
    case Normal = 1000
    //emoji表情
    case Emoji = 1001
    //浪小花表情
    case LXH = 1002
}

class LSEmoticonTooBar: UIStackView {
    
    //点击toolbar按钮执行的闭包
    var didSeletedButtonClosure: ((type: LSEmoticonToolBarButtonType)->())?
    
    //记录上一次的选中按钮
    var lastSeleCtedButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //水平方向布局
        axis = .Horizontal
        //等比填充
        distribution = .FillEqually
        
        addChildButton("默认", imageName: "compose_emotion_table_left", type: .Normal)
        addChildButton("Emoji", imageName: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", imageName: "compose_emotion_table_right", type: .LXH)
        
    }
    
    //MARK: - 添加子按钮的通用方法
    private func addChildButton(title: String, imageName: String, type: LSEmoticonToolBarButtonType) {
        let button = UIButton()
        //根据枚举的原始值作为tag值
        button.tag = type.rawValue
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        button.setTitle(title, forState: .Normal)
        
        //设置不同状态的文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        
        //设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: .Selected)
        
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //去掉高亮
        button.adjustsImageWhenHighlighted = false
        
        addArrangedSubview(button)
        
        if type == .Normal {
            
            lastSeleCtedButton?.selected = false
            button.selected = true
            
            lastSeleCtedButton = button
            
        }
    }
    
    //MARK: - 点击事件
    @objc private func buttonAction(button: UIButton) {
        
        //如果上次点击的选中按钮和这次点击的按钮一样,直接返回,不执行后边的代码
        if lastSeleCtedButton == button {
            return
        }
        
        //上一次的按钮取消选中
        lastSeleCtedButton?.selected = false
        //设置选中状态
        button.selected = true
        //保存选中的按钮
        lastSeleCtedButton = button
        
        //通过枚举的原始值获取枚举
        let type = LSEmoticonToolBarButtonType(rawValue: button.tag)!
        //执行点击闭包
        didSeletedButtonClosure?(type: type)
    }
    
    //MARK: - 通过制定的section选中相应的按钮
    func selectedButtonWithSection(section: Int) {
        
        //通过tag获取相应的按钮
        let button = viewWithTag(section + 1000) as! UIButton
        
        if lastSeleCtedButton?.tag == section + 1000 {
            return
        }
        
        print(section)
        lastSeleCtedButton?.selected = false
        button.selected = true
        lastSeleCtedButton = button
        
    }
}














